package com.intelligentmeterreadingsys.report.activity;

import java.util.List;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.EngineeringOrderPoVo;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

/**
 * 工程单查询主界面
 * @author king
 *
 */
public class OrderQueryMainActivity extends ActivityGroup{

	public LinearLayout layout;
	public RadioGroup group;
	public RadioButton radio1;//工程单信息
	public RadioButton radio2;//工程单列表
	public List<EngineeringOrderPoVo> list;
	public OrderListActivity act;
	private String orderid;
	public String getOrderid() {
		return orderid;
	}

	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_query_main);
		init();
	}

	/**
	 * 初始化控件
	 */
	public void init(){
		layout=(LinearLayout)findViewById(R.id.order_query_main_layout);
		group=(RadioGroup)findViewById(R.id.order_query_main_radio);
		radio1=(RadioButton)findViewById(R.id.order_query_main_orderinfo);
		radio2=(RadioButton)findViewById(R.id.order_query_main_orderlist);
		
		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId==radio1.getId()){
					launchActivity("ReportQueryActivity", ReportQueryActivity.class, null);
				}else if(checkedId==radio2.getId()){
					launchActivity("OrderListActivity", OrderListActivity.class, null);
					 act=(OrderListActivity)getCurrentActivity();
					 
					 ListView listview=act.getListView();
					 list=act.getList();
					 listview.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
								long arg3) {
							if(list==null||list.size()<1){
								Toast.makeText(OrderQueryMainActivity.this, "没有工程单不能进行此操作", 1000).show();
								return;
							}
							
							orderid=list.get((act.getPage()-1)*10+arg2).getId()+"";
							launchActivity("ReportQueryActivity", ReportQueryActivity.class, null);
							radio1.setChecked(true);
							
						}
					});
					
				   
				}
				
			}
		});
		 launchActivity("OrderListActivity", OrderListActivity.class, null);
		 act=(OrderListActivity)getCurrentActivity();
		 
		 ListView listview=act.getListView();
		 list=act.getList();
		 listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if(list==null||list.size()<1){
					Toast.makeText(OrderQueryMainActivity.this, "没有工程单不能进行此操作", 1000).show();
					return;
				}
				
				orderid=list.get((act.getPage()-1)*10+arg2).getId()+"";
				launchActivity("ReportQueryActivity", ReportQueryActivity.class, null);
				radio1.setChecked(true);
				
			}
		});
		 radio2.setChecked(true);
	}
	
	/**
	 * 界面切换
	 * 
	 * @param id
	 * @param activityClass
	 */
	public void launchActivity(String id, Class<?> activityClass, Bundle bundle) {
		layout.removeAllViews();

		Intent intent = new Intent(this, activityClass);
		
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

		Window window = getLocalActivityManager().startActivity(id, intent);
		View view = window.getDecorView();
		layout.addView(view);
	}

}
